﻿
Partial Class WebForm_CommonControl_MySlidingMenuGroup
    Inherits System.Web.UI.UserControl

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Me.BindMenuTitle()
        End If
    End Sub

    Protected Sub rptMenuItem_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rptMenuItem.ItemDataBound
        If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
            Dim ddlMenuItem As DropDownList = TryCast(e.Item.FindControl("ddlMenuItem"), DropDownList)
            Me.BindMenuItem(ddlMenuItem, DataBinder.Eval(e.Item.DataItem, "ProgramID"))
        End If
    End Sub

    Protected Sub ddlMenuItem_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim ddlMenuItem As DropDownList = TryCast(sender, DropDownList)
        If ddlMenuItem.SelectedValue <> "" Then
            Response.Redirect(Me.ResolveUrl(ddlMenuItem.SelectedValue))
        End If
    End Sub

    '繫結標題資料
    Protected Sub BindMenuTitle()
        Dim dat As New clsData
        Dim sql As New MyStringBuilder
        Dim dt As Data.DataTable

        sql.Length = 0
        sql.AppendFormat("select distinct a.ProgramID,a.ProgramName, a.Sequence from Program a ")
        sql.AppendFormat("join RoleProgramMapping b ")
        sql.AppendFormat("	on a.ProgramID=b.ProgramID ")
        sql.AppendFormat("  and b.RoleID in ")
        sql.AppendFormat("  ( select * from fn_GetAllRoleIDByUserID('{0}') ) ", clsAuth.GetUserID)
        sql.AppendFormat("where 1=1 ")
        sql.AppendFormat("	and a.Parent='{0}' ", "0")
        sql.AppendFormat("	and a.Active='{0}' ", "Y")
        sql.AppendFormat("order by a.Sequence ")

        dt = dat.GetData(sql)

        Me.rptMenuTitle.DataSource = dt
        Me.rptMenuTitle.DataBind()

        Me.rptMenuItem.DataSource = dt
        Me.rptMenuItem.DataBind()
    End Sub

    '繫結下拉式選單資料
    Protected Sub BindMenuItem(ByVal ddlMenuItem As DropDownList, ByVal ProgramID As String)
        Dim dat As New clsData
        Dim sql As New MyStringBuilder
        Dim dt As Data.DataTable

        sql.Length = 0
        sql.AppendFormat("Select distinct a.ProgramID,a.ProgramName, a.Sequence ")
        sql.AppendFormat(",a.Url ")
        'sql.AppendFormat(",a.Url+'?ProgramID='+Cast(a.ProgramID as varchar) as Url ")
        sql.AppendFormat("from Program a ")
        sql.AppendFormat("join RoleProgramMapping b ")
        sql.AppendFormat("	on a.ProgramID=b.ProgramID ")
        sql.AppendFormat("  and b.RoleID in ")
        sql.AppendFormat("  ( select * from fn_GetAllRoleIDByUserID('{0}') ) ", clsAuth.GetUserID)
        sql.AppendFormat("where 1=1 ")
        sql.AppendFormat("	and a.Parent='{0}' ", ProgramID)
        sql.AppendFormat("  and a.Active='{0}' ", "Y")
        sql.AppendFormat("order by a.Sequence ")

        dt = dat.GetData(sql)

        ddlMenuItem.DataTextField = "ProgramName"
        ddlMenuItem.DataValueField = "Url"
        ddlMenuItem.DataSource = dt
        ddlMenuItem.DataBind()
        ddlMenuItem.Items.Insert(0, MyObj.GetDDLPleaseChooseItem())

        For i As Integer = 0 To ddlMenuItem.Items.Count - 1
            If Me.Request.RawUrl.Replace("/", "\").IndexOf(ddlMenuItem.Items(i).Value.Replace("~", "")) > 0 Then
                ddlMenuItem.Items(i).Selected = True
                'ddlMenuItem.Items(i).Attributes.Add("style", "background-color:Yellow ")
                ddlMenuItem.Items(i).Attributes.Add("Class", "option_a")
                Exit For
            End If
            '如果都沒有選取,就預設第一個
            If i = ddlMenuItem.Items.Count - 1 Then
                ddlMenuItem.Items(0).Attributes.Add("Class", "option_a")
            End If
        Next
    End Sub
End Class